using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Speed;
using System.Diagnostics;
using ScintillaNET;

namespace SpeedSearch.Forms
{

    public partial class FormView : Form
    {

        public FormView()
        {
            InitializeComponent();
        }

        private void FormView_Load(object sender, EventArgs e)
        {

        }

        public void LoadFilesa(List<string> files)
        {
            tabControl.SuspendLayout();
            //foreach (var file in files)
            //{
            //    TabPage page = new TabPage();
            //    page.Text = Path.GetFileName(file);
            //    page.Tag = false; // indica que ainda não foi carregado
            //    page.ToolTipText = file;
            //    tabControl.Controls.Add(page);
            //}
            TimerCount tc = new TimerCount("tabControl.Controls.AddRange");
            List<TabPage> pages = new List<TabPage>();
            foreach (var file in files)
            {
                TabPage page = new TabPage();
                page.Text = Path.GetFileName(file);
                page.ToolTipText = file;
                page.Tag = file;
                pages.Add(page);
                page.SuspendLayout();
            }
            tabControl.Controls.AddRange(pages.ToArray());
            MessageBox.Show(tc.ToString());

            if (files.Count == 1)
                this.Text = files[0];

            tabControl.ResumeLayout();
            tabControl_SelectedIndexChanged(null, null);
        }

        public void LoadFiles(List<string> files)
        {
            tabControl.SuspendLayout();
            foreach (var file in files)
            {
                TabPage page = new TabPage();
                page.Text = Path.GetFileName(file);
                page.Tag = file;
                Scintilla txt = new Scintilla
                {
                    Dock = DockStyle.Fill,
                    BorderStyle = BorderStyle.None,
                    // Font = new Font("Consolas", 10)
                };

                //var cnf = new ScintillaNET.Configuration.ConfigurationManager();

                //txt.Lexing.Lexer = ScintillaNET.Lexer.Cpp;
                //txt.Lexing.LexerName = "cpp";

                txt.ConfigurationManager.Language = Path.GetExtension(file).Replace(".", "");
                txt.ConfigurationManager.Configure();

                txt.Selection.Mode = ScintillaNET.SelectionMode.Rectangle;


                //TextBox txt = new TextBox
                //{
                //    Dock = DockStyle.Fill,
                //    Multiline = true,
                //    BorderStyle = BorderStyle.None,
                //    ScrollBars = ScrollBars.Both,
                //    WordWrap = false,
                //    Font = new Font("Consolas", 10)
                //};
                
                txt.Tag = file;
                page.Controls.Add(txt);
                page.ToolTipText = file;
                tabControl.Controls.Add(page);
            }

            if (files.Count == 1)
                this.Text = files[0];

            tabControl.ResumeLayout();
            tabControl_SelectedIndexChanged(null, null);
        }

        public static void Show(List<string> files)
        {
            var f = new FormView();
            f.LoadFiles(files);
            f.WindowState = FormWindowState.Maximized;
            //f.SetBounds(0, 0, Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
            f.Show();
        }

        private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            // carrega o texto na demanda. Só quando a tab for ativada pela primeira vez
            try
            {
                this.Cursor = Cursors.WaitCursor;
                TabPage page = (TabPage)tabControl.SelectedTab;
                if (page != null)
                {
                    string file = page.Tag as string;
                    if (!string.IsNullOrEmpty(file))
                    {
                        page.Tag = null;
                        Scintilla txt = (Scintilla)page.Controls[0];
                        if (File.Exists(file))
                        {
                            txt.Text = File.ReadAllText(file);
                        }
                        else
                            txt.Text = "Arquivo não encontrado";
                    }
                }
            }
            catch (Exception ex)
            {
                Program.ShowError(ex);
            }
            this.Cursor = Cursors.Default;
        }

        private void tabControl_SelectedIndexChanged2(object sender, EventArgs e)
        {
            // carrega o texto na demanda. Só quando a tab for ativada pela primeira vez
            try
            {
                this.Cursor = Cursors.WaitCursor;
                TabPage page = (TabPage)tabControl.SelectedTab;
                if (page != null)
                {
                    var isLoaded = (bool)page.Tag;
                    if (!isLoaded)
                    {
                        isLoaded = true;
                        var txt = (Scintilla)page.Controls[0];
                        string file = (string)txt.Tag;
                        if (File.Exists(file))
                            txt.Text = File.ReadAllText(file);
                        else
                            txt.Text = "Arquivo não encontrado";
                    }
                }
            }
            catch (Exception ex)
            {
                Program.ShowError(ex);
            }
            this.Cursor = Cursors.Default;
        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            btnOpenFile.Enabled = false;
            Program.RunSafe(this, () =>
            {
                string file = tabControl.SelectedTab.ToolTipText;
                Process.Start(file);
            });
            btnOpenFile.Enabled = true;
        }

        private void OpenDir_Click(object sender, EventArgs e)
        {
            btnOpenDir.Enabled = false;
            Program.RunSafe(this, () =>
            {
                string file = tabControl.SelectedTab.ToolTipText;
                Process.Start(Path.GetDirectoryName(file));
            });
            btnOpenDir.Enabled = true;
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            Program.RunSafe(this, () =>
            {
                string file = tabControl.SelectedTab.ToolTipText;
                System.Windows.Forms.Clipboard.SetText(file);
            });
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }


    }

}
